<?php
namespace app\member\model;

use app\BaseModel;
use app\integral\model\IntegralGoods;
use app\shop\model\Wholesale;
use think\facade\Db;

class Integral extends BaseModel
{
    //会员积分表
    protected $name = 'longbing_card_member_integral';


    /**
     * @author chenniang
     * @DataTime: 2020-07-15 09:49
     * @功能说明:添加
     */
    public function integralAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-16 16:35
     * @功能说明:列表
     */
    public function integralList($dis,$page){

        $data = $this->where($dis)->order(['update_time desc','id desc'])->paginate($page)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                if($v['controller_type']==0){

                    $v['controller_name'] = '系统核算';

                    $icon = $v['integral_add']>0?'+':'-';

                    $integral_add = $v['integral_add']>0?$v['integral_add']:$v['integral_add']*-1;

                    $text = $v['integral_add']>0?'增加积分':'使用积分';

                    $boj    = $this->returnObj($v['type']);

                    $refund = $v['refund']==1?'(已退款)':'';

                    $v['text'] = $boj.$text.$icon.$integral_add.',现积分 '.$v['integral_after'].$refund;

                }

            }

        }
        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-14 15:17
     * @功能说明:返回一个对象
     */
    public function returnObj($type){


        switch ($type){
            //购买会员商品
            case 0:
                return '升级会员';

                break;
            //会员购买商品
            case 1:
                return '消费获得积分';

                break;
            //购买储值套餐赠送
            case 2:
                return '购买储值套餐';

                break;
            //兑换商品
            case 3:
                return '兑换商品';

                break;
            //兑换商品
            case 4:
                return '兑换卡券';

                break;
            case 5:
                return '兑换商品退款';

                break;
            default:
                return '消费';

                break;
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:08
     * @功能说明:详情
     */
    public function integralInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:13
     * @功能说明:编辑
     */
    public function integralUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-31 10:28
     * @功能说明:增加用户积分
     */
    public function integralUserAdd($user_id,$integral,$uniacid,$status = 2,$type=0,$order_id=0,$atv_id=0,$number=0,$add=1,$order_goods_id=0,$i_goods_id=0){

        if($integral>0||$add==0){

            $member_model = new Member();

            $i_goods_model = new IntegralGoods();

            $member_info  = $member_model->getMemberInfo(['user_id'=>$user_id,'uniacid'=>$uniacid]);

            $insert = [

                'uniacid'           => $uniacid,

                'user_id'           => $user_id,

                'integral_add'      => intval($integral),

                'integral_before'   => intval($member_info['integral']),

                'integral_after'    => intval($member_info['integral']+$integral),

                'status'            => $status,

                'type'              => $type,

                'order_id'          => $order_id,

                'integral_atv_id'   => $atv_id,

                'num'               => $number,

                'order_goods_id'    => $order_goods_id,

                'integral_goods_id' => $i_goods_id,

            ];

            $res = $this->integralAdd($insert);

            $id  = $this->getLastInsID();
            //兑换商品
            if(in_array($type,[3,4,5])){
                //5退货
                $add = $type==5?0:1;

                $res = $i_goods_model->updateStock($i_goods_id,$number,$add);

                if($res==0){

                    return $res;
                }

            }

            return $id;
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-04 10:21
     * @功能说明:每日领取的积分
     */
    public function dayIntegral($user_id,$uniacid){

        $config_model = new Config();

        $config = $config_model->configInfo(['uniacid'=>$uniacid]);

        if($config['integral_limit']==1){

            $dis = [

                'user_id' => $user_id,

                'refund'  => 0,

                'type'    => 1
            ];

            $integral    = $this->where($dis)->where('status','>',0)->whereDay('create_time')->sum('integral_add');

            $re_integral = $config['integral_day_max'] - $integral;

            $re_integral = $re_integral>0?$re_integral:0;

            return intval($re_integral);

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-04 11:07
     * @功能说明:给用户加积分
     */
    public function incUserIntegral($order_id,$user_id,$uniacid,$type=1){

        $dis = [

            'order_id' => $order_id,

            'status'   => 1,

            'type'     => $type
        ];

        $info = $this->where($dis)->select()->toArray();

        $member_model = new Member();

        if(!empty($info)){

            foreach ($info as $value){

                $integral     = $value['integral_add'];

                $member_info  = $member_model->getMemberInfo(['user_id'=>$user_id,'uniacid'=>$uniacid]);

                $update = [
                    //修改状态
                    'status' => 2,
                    //当前
                    'integral_before' => intval($member_info['integral']),
                    //修改后对
                    'integral_after'  => intval($member_info['integral']+$integral),

                ];

                $this->integralUpdate(['id'=>$value['id']],$update);

                $dis = [

                    'user_id' => $user_id

                ];

                $member_model->incDecGrowth($dis,$integral,'integral');


            }

        }
        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-02-01 15:05
     * @功能说明:
     */
    public function DoDiscount($discount_data,$order_goods_id){

    if(!empty($discount_data['i_record_id'])){

        $res = $this->where(['id'=>$discount_data['i_record_id'],'order_goods_id'=>0])->update(['order_goods_id'=>$order_goods_id]);

        return $res;

    }

    return true;


}











}